﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCSoict.Models;
using System.Web.Script.Serialization;

namespace MVCSoict.Controllers
{
	public class StaffsController : Controller
	{
		private StaffDbContext db = new StaffDbContext();

		// GET: /Staffs/
		public ActionResult Index()
		{
			return View(db.Staffs.ToList());
		}

		// GET: /Staffs/Details/5
		public ActionResult Details(string ID)
		{
			if (ID == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Staff staff = db.Staffs.Find(ID);
			if (staff == null)
			{
				return HttpNotFound();
			}
			return View(staff);
		}

		// GET: /Staffs/Create
		public ActionResult Create()
		{
			return View();
		}

		// POST: /Staffs/Create
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		//[ValidateAntiForgeryToken]
		public ActionResult Create([Bind(Include = "ID,Department,Name,Room")] Staff staff)
		{
			if (ModelState.IsValid)
			{
				db.Staffs.Add(staff);
				db.SaveChanges();
				return RedirectToAction("Index");
			}

			return View(staff);
		}

		class GetStaffParam
		{
			public int Page;
			public int PageSize;
		}
		/// <summary>
		/// Lấy danh asch1 các cán bộ
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		public string Get(string p)
		{
			return "";
		}

		public string Detail(string ID)
		{
			return "";
		}


		public string Update(Staff staff)
		{
			
	

			return new JavaScriptSerializer().Serialize(new { Code = 0, String = "successed" });
		}

		// GET: /Staffs/Edit/5
		public ActionResult Edit(string id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Staff staff = db.Staffs.Find(id);
			if (staff == null)
			{
				return HttpNotFound();
			}
			return View(staff);
		}

		// POST: /Staffs/Edit/5
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Edit([Bind(Include = "ID,Department,Name,Room")] Staff staff)
		{
			if (ModelState.IsValid)
			{
				db.Entry(staff).State = EntityState.Modified;
				db.SaveChanges();
				return RedirectToAction("Index");
			}
			return View(staff);
		}

		class RemoveParam
		{
			public string ID;
		}

		/// <summary>
		/// Xóa nhân viên dựa theo ID
		/// </summary>
		/// <param name="p"></param>
		/// <returns>
		/// {
		/// Success: false;
		/// ErrorString: "user không tồn tại"
		/// }</returns>
		public string Remove(string p)
		{
			return "";
		}

		class CheckUsernameParam
		{
			public string Username;
		}

		/// <summary>
		/// Kiểm tra xem username đã tồn tại hay chưa
		/// </summary>
		/// <param name="p"></param>
		/// <returns>
		/// {
		///	Existed: false
		/// }</returns>
		public string CheckUsername(string p)
		{
			return "";
		}
		class LoginParam
		{
			public string Username;
			public string Password;
		}
		public string Login(string p)
		{
			return "";
		}

		public string Logout(string p)
		{
			return "";
		}

		class RegisterParam
		{
			public string Username;
			public string Password;
			public string Email;
		}
		public string Register(string p)
		{
			return "";
		}

		class ResetPasswordStaffParam
		{
			public string OldPassword;
			public string NewPassword;
		}
		public string ResetPassword(string p)
		{
			return "";
		}

		class CheckPasswordParam
		{
			public string Password;
		}
		public string CheckPassword(string p)
		{
			return "";
		}

		// GET: /Staffs/Delete/5
		public ActionResult Delete(string id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Staff staff = db.Staffs.Find(id);
			if (staff == null)
			{
				return HttpNotFound();
			}
			return View(staff);
		}

		// POST: /Staffs/Delete/5
		[HttpPost, ActionName("Delete")]
		[ValidateAntiForgeryToken]
		public ActionResult DeleteConfirmed(string id)
		{
			Staff staff = db.Staffs.Find(id);
			db.Staffs.Remove(staff);
			db.SaveChanges();
			return RedirectToAction("Index");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				db.Dispose();
			}
			base.Dispose(disposing);
		}
	}
}
